package com.xiaoc.bulldozer.controller;

import com.xc.framework.common.base.ResponseCode;
import com.xiaoc.bulldozer.common.model.Priviledge;
import com.xiaoc.bulldozer.common.model.Role;
import com.xiaoc.bulldozer.common.model.User;
import com.xiaoc.bulldozer.common.service.ActionService;
import com.xiaoc.bulldozer.common.service.PriviledgeService;
import com.xiaoc.bulldozer.common.service.RoleService;
import com.xiaoc.bulldozer.facade.PriviledgeFacade;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 角色权限管理
 * 
 * @author mayong
 */
@Controller
@RequestMapping(value = "/priviledge")
public class FunctionRoleController extends AdminBaseController {

    @Resource(name = "priviledgeFacade")
    private PriviledgeFacade priviledgeFacade;

    @Resource(name = "roleService")
    private RoleService roleService;

    @Resource(name = "actionService")
    private ActionService actionService;

    @Resource(name = "priviledgeService")
    private PriviledgeService priviledgeService;

    /**
     * 列举所有角色以及对应的所有权限
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ModelAndView list() {

        // 获取全部角色
        List<Role> roleList = roleService.getAllRoleList();

        // 获取全部的角色权限分组
        Map<Integer, Map<String, Object>> priviledgeMap = priviledgeFacade.getAllRolePriviledge();

        Map<String, Object> argsParam = new HashMap<String, Object>();
        argsParam.put("priviledgeMap", priviledgeMap);
        argsParam.put("roleList", roleList);
        return renderHtml("user/function_rights_list.html", argsParam);
    }

    /**
     * 跳转到新增或修改页面
     * 
     * @param roleId
     */
    @RequestMapping(value = "/gotoAddOrUpdate", method = RequestMethod.GET)
    public ModelAndView gotoAddOrUpdate(Integer roleId) {

        // 获取全部的 Action
        Map<String, Object> actionMap = priviledgeFacade.getAllAction();

        String roleName = null;
        Map<Integer, Priviledge> priviledgeMap = new HashMap<Integer, Priviledge>();
        if (roleId != null) {
            Role role = roleService.getRoleById(roleId);
            roleName = role.getName();

            // 获取全部的角色权限分组
            priviledgeMap = priviledgeFacade.getPrivildegeByRoleId(roleId);
        }

        Map<String, Object> argsParam = new HashMap<String, Object>();
        argsParam.put("actionMap", actionMap);
        argsParam.put("roleId", roleId);
        argsParam.put("roleName", roleName);
        argsParam.put("priviledgeMap", priviledgeMap);
        return renderHtml("user/function_rights_mg.html", argsParam);
    }

    /**
     * 更新角色对应的权限
     */
    @RequestMapping(value = "/addOrUpdate", method = RequestMethod.POST)
    public ModelAndView addOrUpdate(HttpServletRequest request, HttpServletResponse response, Integer roleId, String roleName, Integer[] actionIdArr) {

        User user = getCurrentLoginUser(request, response);

        Role role = null;
        if (roleId == null) {
            role = new Role();
            role.setName(roleName);
            role.setUpdateUser(user.getId());
            roleService.save(role);
        } else {

            role = roleService.getRoleById(roleId);
            // 角色不存在
            if (role == null) {
                return jsonFailed(ResponseCode.INTERNAL_ERROR, "role not exist.");
            }

            role.setName(roleName);
            role.setUpdateUser(user.getId());
            roleService.update(role);
        }

        priviledgeFacade.updateRolePriviledge(actionIdArr, role);

        // 重定向
        return redirect(getRequest().getContextPath() + "/priviledge/list");
    }

}